<template>
    <div style="text-align:center;">
        <el-card class="box-card">
            <template #header>
            <div class="card-header">
                <span style="width: 100%;">大佬请登录</span>
            </div>
            </template>
            
            <el-form
                ref="ruleFormRef"
                :model="ruleForm"
                status-icon
                :rules="rules"
                label-width="120px"
                class="demo-ruleForm"
            >
            <el-form-item label="Username" prop="username">
            <el-input v-model.number="ruleForm.username" />
            </el-form-item>
            <el-form-item label="Password" prop="pass">
            <el-input v-model="ruleForm.pass" type="password" autocomplete="off" />
            </el-form-item>
                <el-form-item>
                <el-button type="primary" @click="submitForm(ruleFormRef)"
                    >Submit</el-button
                >
                <el-button @click="resetForm(ruleFormRef)">Reset</el-button>
                </el-form-item>
            </el-form>
        </el-card>
    </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { useStore } from 'vuex'
import { key } from '@/store/store';
import axios from '../../../request'
import { ElMessage,FormInstance, FormRules } from 'element-plus'
import { UserInfo } from "../../../type/UserInfo";
import router from '@/router';

const ruleFormRef = ref<FormInstance>()

const validateUsername = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('Please input the username'))
  } else {
    callback()
  }
}

const validatePass = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('Please input the password'))
  } else {
    callback()
  }
}

const ruleForm = reactive({
  pass: '',
  username: '',
})

const rules = reactive<FormRules<typeof ruleForm>>({
  pass: [{ validator: validatePass, trigger: 'blur' }],
  username: [{ validator: validateUsername, trigger: 'blur' }],
})

const store = useStore(key)

const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      console.log('submit!')
      // debugger
      const parms = axios.post<UserInfo>("/api/login",ruleForm);
      parms.then((res)=>{
        // console.log(res);
        // 将token信息添加到store
        if (res.data?.username) {
          store.state.username = res.data.username
          sessionStorage.setItem("username", res.data?.username);
          sessionStorage.setItem("token", res.data?.token || '');
          router.go(0)
        }else {
          ElMessage.error('用户名或密码错误.')
          return false
        }
      });

    } else {
      console.log('error submit!')
      return false
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}
</script>

<style>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.item {
  margin-bottom: 18px;
}

.box-card {
  width: 480px;
}
</style>